//
//  MainMenuVC.m
//  Mockup
//
//  Created by JOHN HODGSON on 2/14/10.
//  Copyright 2010 Mac-Easy@USC. All rights reserved.
//
//  Last update: 3/2/10


#import "MainMenuVC.h"
//#import "AboutVC.h"
#import "Sound.h"
#import "Board.h"
#import "OptionsVC.h"

@implementation MainMenuVC

//@synthesize background, title, 
//@synthesize aboutController;
@synthesize highScoresController;
@synthesize gameController;
@synthesize optionsViewController;
@synthesize loadButton;

- (BOOL)canBecomeFirstResponder{
	return YES;
}

-(IBAction)NewButtonPressed:(id)sender{
	NSLog(@"NewGame button pressed!");
	[self newGameAction];
}
-(IBAction)LoadButtonPressed:(id)sender{
	NSLog(@"LoadGame button pressed!");
	[self loadGameAction];
}
-(IBAction)HighScoreButtonPressed:(id)sender{
	NSLog(@"HighScore button pressed!");
	[self highScoreAction];
	HighScoresVC *hsView = [[HighScoresVC alloc] initWithNibName:nil bundle:nil];
	[self presentModalViewController:hsView animated:YES];
}
-(IBAction)HelpButtonPressed:(id)sender{
	NSLog(@"Help button pressed!");
	[self helpAction];
	
	HelpVC *helpVC = [[HelpVC alloc] initWithNibName:nil bundle:nil];
	[self presentModalViewController:helpVC animated:YES];
}
-(IBAction)OptionsButtonPressed:(id)sender{
	NSLog(@"Options button pressed!");
	[self optionsAction];
	OptionsVC *optionsView = [[OptionsVC alloc] initWithNibName:nil bundle:nil];
	[self presentModalViewController:optionsView animated:YES];
}
-(IBAction)AboutButtonPressed:(id)sender{
	NSLog(@"About button pressed!");
	[self aboutAction];
	
	AboutVC *aboutView = [[AboutVC alloc] initWithNibName:nil bundle:nil];
	[self presentModalViewController:aboutView animated:YES];
}

-(void)showSplashScreen{
	UIViewController *modalViewController = [[UIViewController alloc] init];
	modalViewController.view = splashView;
	[self presentModalViewController:modalViewController animated:NO];
	[self performSelector:@selector(hideSplashScreen) withObject:nil afterDelay:1.0];
}

-(void)hideSplashScreen{
	[[self modalViewController] dismissModalViewControllerAnimated:YES];
}

-(void)newGameAction{
	NSLog(@"Inside the newGameAction method.");
	// test
	[self checkSavedGame];
	if ([Configuration savedGame]) {
		[self overrideAction];
	} else {
		[self showDifficultySelector];
	}
	
}

-(void)loadGameAction{
	NSLog(@"Inside the loadGameAction method.");
	if ([Configuration savedGame]) {
		[self startGameWithContentOfFile:[Configuration getSavedGameFile]];
	}
}

-(void)highScoreAction{
	NSLog(@"Inside the highScoreAction method.");
}

-(void)helpAction{
	NSLog(@"Inside the helpAction method.");
}

-(void)optionsAction{
	NSLog(@"Inside the optionsAction method.");
}

-(void)aboutAction{
	NSLog(@"Inside the aboutAction method.");
}

-(BOOL)checkSavedGame{
	NSLog(@"Inside the checkSaveGame method.");
	//[self overrideAction];
	return [Configuration savedGame];
}

-(void)showDifficultySelector{
	NSLog(@"Inside the showDifficultySelector method.");
	UIAlertView *difficultySelector = [[UIAlertView alloc] initWithTitle:@"New Game!" 
																 message:@"Choose a difficulty level" 
																delegate:self cancelButtonTitle:@"Cancel" 
													   otherButtonTitles:@"Easy", @"Medium", @"Hard", nil];
	[difficultySelector setTag:DifficultySelector];
	[difficultySelector show];
	[difficultySelector release];
}

-(void)showOverrideConfirmation{
	NSLog(@"Inside the showOverrideConfirmation method.");
}


-(void)continueAction{
	NSLog(@"Inside the continueAction method.");
	[self startGameWithContentOfFile:[Configuration getSavedGameFile]];
}

-(void)overrideAction{
	NSLog(@"Inside the overrideAction method.");
	UIAlertView * saveGameAlert = [[UIAlertView alloc] initWithTitle:nil 
															 message:@"You have a saved game in progress" 
														    delegate:self cancelButtonTitle:@"CANCEL" 
												   otherButtonTitles:@"CONTINUE", @"NEW GAME", nil];
	[saveGameAlert setTag:GameOverrideAlert];
	[saveGameAlert show];
	[saveGameAlert release];
	
	
}


/**
 From what I understand, this is a catch-all method that should handle all of the alerts in the
 MainMenuVC. Any time a alertView is resolved by pressing a button, this method gets called with
 the appropriate alertView tag and buttonIndex.
 **/
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	if([alertView tag] == GameOverrideAlert){
		switch (buttonIndex) {
			case 0: //Cancel
				break;
			case 1: //User elects to continue a saved game.
				[self continueAction];
				break;
			case 2: //User elects to make a new game.
				//This should go to the next stage of starting a new game: selecting difficulty.
				[self showDifficultySelector];
				break;
		}
	} else if([alertView tag] == DifficultySelector) {
		switch (buttonIndex) {
			case 0: //Cancel
				break;
			case 1: //User selected "easy" mode
				[self startGameWithDifficulty: Easy];
				break;
			case 2: //User selected  "medium" mode
				[self startGameWithDifficulty: Medium];
				break;
			case 3: //User selected "hard" mode
				[self startGameWithDifficulty: Hard];
				break;
		}
	}
}

-(void) startGameWithDifficulty:(difficulty_t) difficulty{
	[Configuration setPreferredDifficulty:difficulty];
	GameVC * gameView = [[GameVC alloc] initWithDifficulty:difficulty];
	[self presentModalViewController:gameView animated:YES];
	[self.navigationController pushViewController:gameController animated:YES];
	[gameView release];
}

-(void) startGameWithContentOfFile:(NSString *)filename {
	GameVC * gameView = [[GameVC alloc] initWithContentOfFile:filename];
	[self presentModalViewController:gameView animated:YES];
	[self.navigationController pushViewController:gameController animated:YES];
	[gameView release];
}

/**
 -(void)transToGameBoard:(int)diff{
 Board *newGameBoard = [[[Board alloc] initWithRandomMatrix:diff] autorelease];
 }
 **/

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[Sound initMusic];
    [super viewDidLoad];
	
	self.loadButton.enabled = [Configuration savedGame]; 
}

- (void)viewDidAppear:(BOOL)animated{
	self.loadButton.enabled = [Configuration savedGame];
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
